package com.medusa.aps.business.modules.basic.controller;


import com.medusa.aps.business.model.pagination.Page;
import com.medusa.aps.business.modules.basic.model.input.MaterialClassificationDTO;
import com.medusa.aps.business.modules.basic.mp.entity.MaterialClassification;
import com.medusa.aps.business.modules.basic.mp.service.MaterialClassificationService;
import com.medusa.aps.business.common.model.resp.Result;
import com.medusa.aps.business.common.web.valid.group.Update;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import jakarta.validation.constraints.Size;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 物料分类控制层
 *
 * @author yuxh
 * @since 2023-08-14 17:37:47
 */
@Validated
@RestController
@RequestMapping("/materialClassification")
@Tag(name = "物料分类管理", description = "物料分类管理")
public class MaterialClassificationController {
    /**
     * 服务对象
     */
    @Resource
    private MaterialClassificationService materialClassificationService;

    /**
     * 分页查询物料分类
     *
     * @param page               分页对象
     * @param classificationName 物料分类名称
     * @return 分页对象
     */
    @GetMapping("/selectAll")
    @Operation(summary = "分页查询所有数据")
    public Result<Page<MaterialClassification>> pageMaterialClassification(Page<MaterialClassification> page,
                                                                           String classificationName) {
        return Result.ok(materialClassificationService.pageMaterialClassification(page, classificationName));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("/selectOne")
    @Operation(summary = "通过主键查询单条数据")
    public Result<MaterialClassification> selectOne(@RequestParam Integer id) {
        return Result.ok(materialClassificationService.getById(id));
    }

    /**
     * 新增数据
     *
     * @param materialClassificationDTOS 实体对象
     * @return 新增结果
     */
    @PostMapping("/save")
    @Operation(summary = "新增数据")
    public Result<Void> saveMaterialClassification(@RequestBody @Valid @Size(min = 1, max = 50, message = "物料分类最多添加50条") List<MaterialClassificationDTO> materialClassificationDTOS) {
        materialClassificationService.saveMaterialClassification(materialClassificationDTOS);
        return Result.ok();
    }

    /**
     * 修改数据
     *
     * @param materialClassificationDTO 实体对象
     * @return 修改结果
     */
    @PutMapping("/update")
    @Operation(summary = "修改数据")
    public Result<Boolean> updateMaterialClassification(@RequestBody @Validated(Update.class) MaterialClassificationDTO materialClassificationDTO) {
        return Result.ok(materialClassificationService.updateMaterialClassification(materialClassificationDTO));
    }

    /**
     * 删除数据
     *
     * @param ids id集合
     */
    @DeleteMapping("/delete")
    @Operation(summary = "删除数据")
    public Result<Void> delete(@RequestBody List<Long> ids) {
        materialClassificationService.deleteMaterialClassification(ids);
        return Result.ok();
    }
}

